/**
 * 
 */
package com.tomagency.cyberlab.client.activity;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.tomagency.cyberlab.client.ClientFactory;
import com.tomagency.cyberlab.client.ui.ObjectListenView;
import com.tomagency.cyberlab.client.ui.ScannerAppView;

import de.gammadata.gwt.modelle.ResultObject;

/**
 * @author gfr
 *
 */
public class ScanAppActivity extends AbstractActivity implements ScannerAppView.Presenter{
	
	ClientFactory clientFactory;
	ScannerAppView view;
	
	public ScanAppActivity(ClientFactory clientFactory){
		
		this.clientFactory=clientFactory;
		
	}

	/* (non-Javadoc)
	 * @see com.google.gwt.activity.shared.Activity#start(com.google.gwt.user.client.ui.AcceptsOneWidget, com.google.gwt.event.shared.EventBus)
	 */
	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		view=clientFactory.getScannerAppView();
		view.setPresenter(this);
		panel.setWidget(view.asWidget());
		fetchStatus();

	}
	
	private void fetchStatus(){
		view.setMessage("hole Status vom Server");
		clientFactory.getRpcServiceProvider().getScannerService().getTaopixScannerStatus(new AsyncCallback<String>(){

			@Override
			public void onFailure(Throwable caught) {
				view.setMessage("Fehler "+caught.getMessage());
				
			}

			@Override
			public void onSuccess(String result) {
				view.setMessage(result);
				
			}
			
		});
	}
	
	private void doStartStop(boolean start){
		if (start)
			view.setMessage("Fordere Start-Scan am Server an");
		else
			view.setMessage("Fordere Stopp-Scan am Server an");
		
		clientFactory.getRpcServiceProvider().getScannerService().changeSannerRunModus(start,new AsyncCallback<ResultObject>(){

			@Override
			public void onFailure(Throwable caught) {
				view.setMessage("Fehler "+caught.getMessage());
				
			}

			@Override
			public void onSuccess(ResultObject result) {
				view.setMessage(result.getMessage());
				
			}
			
		});
		
	}

	@Override
	public void onRescanClicked() {
		view.setMessage("Fordere Scan am Server an");
		clientFactory.getRpcServiceProvider().getScannerService().doImmediateScan(new AsyncCallback<ResultObject>(){

			@Override
			public void onFailure(Throwable caught) {
				view.setMessage("Fehler "+caught.getMessage());
				
			}

			@Override
			public void onSuccess(ResultObject result) {
				view.setMessage(result.getMessage());
				
			}
			
		});
		
	}

	@Override
	public void onStartClicked() {
		doStartStop(true);
		
	}

	@Override
	public void onStatusClicked() {
		fetchStatus();
		
	}

	@Override
	public void onStopClicked() {
		doStartStop(false);
		
	}

}
